//
//  CheckUnPassViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/11/9.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "CheckUnPassViewController.h"
#import "UnPassModel.h"
#import "UnPassTableViewCell.h"
#import "SubjectNoEditViewController.h"
#import "SubjectCheckEditViewController.h"
#import "SubjectStoreEditViewController.h"
#import "ChooseView.h"
#import "RegisterShopModel.h"
#import "SubjectContentModel.h"
@interface CheckUnPassViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView*homeTableView;

@property(nonatomic,strong)UnPassModel *model;

@property(nonatomic,strong)NSMutableDictionary *params;

@property(nonatomic,strong)NSMutableArray *PopArr;

@property(nonatomic,strong)UIButton*RightBtn;

@end

@implementation CheckUnPassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.params=[NSMutableDictionary dictionary];
    
    [self.params setValue:@"0" forKey:@"state"];
    
    self.title=@"不符合项";
    
    [self.view addSubview:self.homeTableView];
    
    [self loadDatas];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:self.RightBtn];
    
    for (NSInteger i=0; i<5; i++) {
        
        RSItems *item=[RSItems new];
        
        switch (i) {
            case 0:
            {
                item.name=@"全部";
                
                item.value=@"0";
            }
                break;
            case 1:
            {
                item.name=@"未处理";
                
                item.value=@"1";
            }
                break;
            case 2:
            {
                item.name=@"待审核";
                
                item.value=@"2";
            }
                break;
            case 3:
            {
                item.name=@"已通过";
                
                item.value=@"3";
            }
                break;
            case 4:
            {
                item.name=@"未通过";
                
                item.value=@"4";
            }
                break;
                
            default:
                break;
        }
        
        [self.PopArr addObject:item];
    }
    
    // Do any additional setup after loading the view.
}

-(UIButton*)RightBtn
{
    if (!_RightBtn) {
        
        _RightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _RightBtn.frame = CGRectMake(0, 0, 30, 20);
        [_RightBtn setTitle:@"全部" forState:UIControlStateNormal];
        [_RightBtn addTarget:self action:@selector(PopView:) forControlEvents:UIControlEventTouchUpInside];
        _RightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_RightBtn setTitleColor:TBColorWhite forState:UIControlStateNormal];
       
        [_RightBtn sizeToFit];
  
    }
    return _RightBtn;
}


-(NSMutableArray*)PopArr
{
    if (!_PopArr) {
        
        _PopArr=[NSMutableArray array];
        
    }
    
    return _PopArr;
}


-(TB_TableView*)homeTableView
{
    if (!_homeTableView) {
        
        _homeTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
        
        _homeTableView.delegate=self;
        
        _homeTableView.dataSource=self;
        
        _homeTableView.estimatedRowHeight=70;
        
        [self configureNeedShowEmptyDataSetScrollView:_homeTableView];
        
          [_homeTableView registerNib:[UINib nibWithNibName:@"UnPassTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        _homeTableView.tableFooterView=[[UIView alloc]init];

    }
    
    return _homeTableView;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.model.data.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UnPassData *data=self.model.data[indexPath.row];
    
    UnPassTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.data=data;
    
    return cell;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewAutomaticDimension;
    
}


-(void)loadDatas
{
    
    [self showLoadingAnimation];
    
    BaseReqApi *Api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/inspectNonconformityList.json" andrequestTime:5 andParams:@{@"resultId":self.resultId,@"params":[HelpWay dictionaryToJSON:self.params]} andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [Api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
        
        [self stopLoadingAnimation];
        
        self.model=[UnPassModel mj_objectWithKeyValues:responseObject];
        
        if (responseStatus==1) {
            //请求成功
            
            if (self.model.data.count==0) {
                
                self.Type=TBNODateType;
                
            }
            [self.homeTableView reloadData];
            
        }else if (responseStatus==0||responseStatus==2)
        {
            [MBProgressHUD showErrorMessage:message];
            
              self.Type=TBReqFalie;
            
        }else{
            //404需要重新登录了
            
            [UIAlertView bk_showAlertViewWithTitle:message message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                if (buttonIndex==1) {
                    
                    [self GotoLoginVC];
                }
                
            }];
            
        }
        
    }];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UnPassData *data=self.model.data[indexPath.row];
    
    SubjectLogs *logs=[SubjectLogs new];
    
    logs.name=data.name;
    
    logs.logId=data.logId;
    
    logs.lev=data.lev;
        
    if ([self getUserType]==1) {
        
        //商户角色 都可以编辑吧!
        SubjectStoreEditViewController *edit=[SubjectStoreEditViewController new];
        
        edit.logs=logs;
        
        edit.block=^()
        {
            [self loadDatas];
            
        };
        
        [self.navigationController pushViewController:edit animated:YES];
        
    }else if ([self getUserType]==2)
    {
        
        //未确认
        SubjectCheckEditViewController *edit=[SubjectCheckEditViewController new];
        
        edit.logs=logs;
        
        edit.block=^()
        {
            [self loadDatas];
            
        };
        
        [self.navigationController pushViewController:edit animated:YES];
        
        
    }else if ([self getUserType]==3)
    {
        //开办方 自己的记录和开办方一样，商户记录只能浏览未通过的
        
        if (self.OpenLookOther) {
            
            //开办方查看商户
            
            SubjectNoEditViewController *NoEdit=[SubjectNoEditViewController new];
            
            NoEdit.logs=logs;
            
            [self.navigationController pushViewController:NoEdit animated:YES];
            
        }else{
            
            SubjectStoreEditViewController *edit=[SubjectStoreEditViewController new];
            
            edit.logs=logs;
            
            edit.block=^()
            {
                [self loadDatas];
                
            };
            [self.navigationController pushViewController:edit animated:YES];
            
        }

        
    }else if ([self getUserType]==4)
    {
        //管理员 所有的只能浏览未通过的
        
        SubjectNoEditViewController *NoEdit=[SubjectNoEditViewController new];
        
        NoEdit.logs=logs;
        
        [self.navigationController pushViewController:NoEdit animated:YES];
        
    }
    
    
}


-(void)PopView:(UIBarButtonItem*)btn
{
    
    //躺框
    ChooseView *chooseView=[[ChooseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kDevice_Is_iPhoneX?SCREEN_HEIGHT-64-IPhoneXTableHeight:SCREEN_HEIGHT-64) andDataArr:self.PopArr andBlock:^(NSString *value, NSInteger index) {
        
          [self.params setValue:value forKey:@"state"];
        
        RSItems *item=self.PopArr[index];
        
        [self loadDatas];
        
        [self.RightBtn setTitle:item.name forState:UIControlStateNormal];
        
        [self.RightBtn sizeToFit];
 
        
    }];
    
    [chooseView PopWithView:self.view];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
